//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Vector;
import javax.swing.JPanel;

public class GameJPanel extends JPanel implements KeyListener, Runnable{
    MyTank mytank = null;
    String project = System.getProperty("user.dir");
    Vector<EnemyTank> enemyTankVector = new Vector();

    boolean END=false;
    Image inmage=Toolkit.getDefaultToolkit().createImage(project+"\\src\\gongzhu.png");
    Image yongsheng1=Toolkit.getDefaultToolkit().createImage(project+"\\src\\永胜下.png");
    Image yongsheng2=Toolkit.getDefaultToolkit().createImage(project+"\\src\\永胜上.png");
    Image yongsheng3=Toolkit.getDefaultToolkit().createImage(project+"\\src\\永胜右.png");
    Image yongsheng4=Toolkit.getDefaultToolkit().createImage(project+"\\src\\永胜左.png");
    Image shibai=Toolkit.getDefaultToolkit().createImage(project+"\\src\\失败.jpg");
    Image shengli=Toolkit.getDefaultToolkit().createImage(project+"\\src\\胜利.jpg");
    Image inmage1=Toolkit.getDefaultToolkit().createImage(project+"\\src\\倪总.png");
    GameJPanel() {
        //画出我方坦克
        this.mytank = new MyTank(20, 30);
        //画出敌方坦克...

        for(int i = 0; i < 3; ++i) {
            EnemyTank enemyTank = new EnemyTank(i * 400 + 50, 650-i*300);
            this.enemyTankVector.add(enemyTank);
            Thread thread = new Thread(enemyTank);
            thread.start();
        }
    }
    public void paint(Graphics g) {
        super.paint(g);
        g.setColor(Color.GRAY);
        g.fillRect(0, 0, 1500, 800);
        checkMyTank(mytank);
//        g.drawImage(inmage1,50, 50,200,200,this);
        g.setColor(Color.BLACK);
        Font font = new Font("宋体", Font.BOLD, 25);
        g.setFont(font);
        g.setColor(Color.RED);
        g.drawString("您累计击毁敌方坦克", 1020, 30);
        if(enemyTankVector.size()==3){
            g.drawString("0",1120,60);
        }else if(enemyTankVector.size()==2){
            g.drawString("1",1120,60);
        }else if(enemyTankVector.size()==1){
            g.drawString("2",1120,60);
        }else if(enemyTankVector.size()==0){
            g.drawString("0",1120,60);
        }
        g.drawString("剩余最后一辆敌方坦克", 1000, 90);
        g.drawString("自动释放技能",1050,120);
        if(mytank.isLife()){
            this.drawTank(this.mytank.getX(), this.mytank.getY(), g, this.mytank.getDirection(), this.mytank.getType());
            if(enemyTankVector.size()==1){
                if(this.mytank.getDirection()==0){
                    g.drawImage(yongsheng1,this.mytank.getX()+20, this.mytank.getY()+100,75,75,this);
                }else if(this.mytank.getDirection()==1){
                    g.drawImage(yongsheng2,this.mytank.getX()+20, this.mytank.getY()-70,75,75,this);
                }else if(this.mytank.getDirection()==2){
                    g.drawImage(yongsheng3,this.mytank.getX()+100, this.mytank.getY()+20,75,75,this);
                }else if(this.mytank.getDirection()==3){
                    g.drawImage(yongsheng4,this.mytank.getX()-80, this.mytank.getY()+20,75,75,this);
                }
            }
        }

        for(int i = 0; i < enemyTankVector.size(); i ++) {
            EnemyTank enemyTank = (EnemyTank)this.enemyTankVector.get(i);
            checkEnemyTank(enemyTank);
            if(enemyTank.isLife()&&END==false){
                this.drawTank(enemyTank.getX(), enemyTank.getY(), g, enemyTank.getDirection(), enemyTank.getType());
                for(int j = 0; j < enemyTank.bulletvector.size(); j ++){
                    Bullet bullet = enemyTank.bulletvector.get(j);
                    check(bullet);
                    if(bullet.isLeft()&&END==false){
                        g.drawImage(inmage1,bullet.getX()-10, bullet.getY()-10,50,50,this);
                    }else{
                        enemyTank.bulletvector.remove(bullet);
                    }
                }
            }else{
                enemyTankVector.remove(enemyTank);
                if(enemyTankVector.size() == 0) {
                    END = true;
                }
            }
        }


        if(END==true){
            g.drawImage(shengli,0, 0,1500,800,this);
        }
        if(mytank.isLife()){
            for(int i = 0; i < mytank.bulletvector.size(); i ++){
                Bullet bullet = mytank.bulletvector.get(i);
                check(bullet);
                if(bullet.isLeft()&&END==false){
                        //画出公主头
                        g.drawImage(inmage,bullet.getX()-10, bullet.getY()-10,50,50,this);

                }else{
                    mytank.bulletvector.remove(bullet);
                }
            }
        }else{
            g.drawImage(shibai,0, 0,1500,800,this);
        }
//        g.drawImage(yongsheng1,100, 200,100,100,this);
//        g.drawImage(yongsheng2,100, 0,100,100,this);
//        g.drawImage(yongsheng3,200, 100,100,100,this);
//        g.drawImage(yongsheng4,0, 100,100,100,this);
    }

    //键盘监视器
    class KeyMonitor extends KeyAdapter {
        //按下键盘
        @Override
        public void keyPressed(KeyEvent e) {
            System.out.println(e.getKeyChar());
        }
        //返回键盘
    }
    public void drawTank(int x, int y, Graphics g, int dir, int type) {
        if(type==0){
            g.setColor(Color.pink);
        }else{
            g.setColor(Color.orange);
        }

//0,1,2,3   下上右左
        switch (dir) {
            case 0:
                g.fillOval(x + 5, y + 10, 100, 100);
                g.setColor(Color.white);
                g.fillOval(x + 20, y + 11, 20, 20);
                g.fillOval(x + 60, y + 11, 20, 20);
                g.setColor(Color.BLACK);
                g.fillOval(x + 20, y + 11, 10, 10);
                g.fillOval(x + 60, y + 11, 10, 10);
                g.setColor(Color.red);
                g.fillOval(x + 44, y + 70, 20, 60);
                break;
            case 1:
                g.fillOval(x + 5, y + 10, 100, 100);
                g.setColor(Color.white);
                g.fillOval(x + 20, y + 89, 20, 20);
                g.fillOval(x + 60, y + 89, 20, 20);
                g.setColor(Color.BLACK);
                g.fillOval(x + 30, y + 99, 10, 10);
                g.fillOval(x + 70, y + 99, 10, 10);
                g.setColor(Color.red);
                g.fillOval(x + 44, y - 20, 20, 60);
                break;
            case 2:
                g.fillOval(x + 5, y + 10, 100, 100);
                g.setColor(Color.white);
                g.fillOval(x + 9, y + 30, 20, 20);
                g.fillOval(x + 9, y + 70, 20, 20);
                g.setColor(Color.BLACK);
                g.fillOval(x + 9, y + 40, 10, 10);
                g.fillOval(x + 9, y + 80, 10, 10);
                g.setColor(Color.red);
                g.fillOval(x + 70, y + 44, 60, 20);
                break;
            case 3:
                g.fillOval(x + 5, y + 10, 100, 100);
                g.setColor(Color.white);
                g.fillOval(x + 89, y + 30, 20, 20);
                g.fillOval(x + 89, y + 70, 20, 20);
                g.setColor(Color.BLACK);
                g.fillOval(x + 99, y + 30, 10, 10);
                g.fillOval(x + 99, y + 70, 10, 10);
                g.setColor(Color.red);
                g.fillOval(x - 26, y + 44, 60, 20);
                break;
        }
    }
@Override
    public void keyTyped(KeyEvent e) {
    }
@Override
    public void keyPressed(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_W||e.getKeyCode() == KeyEvent.VK_UP) {
            this.mytank.setDirection(1);
            this.mytank.up();
        } else if (e.getKeyCode() == KeyEvent.VK_D||e.getKeyCode() == KeyEvent.VK_RIGHT) {
            this.mytank.setDirection(2);
            this.mytank.right();
        } else if (e.getKeyCode() == KeyEvent.VK_A||e.getKeyCode() == KeyEvent.VK_LEFT) {
            this.mytank.setDirection(3);
            this.mytank.left();
        } else if (e.getKeyCode() == KeyEvent.VK_S||e.getKeyCode() == KeyEvent.VK_DOWN) {
            this.mytank.setDirection(0);
            this.mytank.down();
        }
        else if(e.getKeyCode() == KeyEvent.VK_J){
            if(mytank.bulletvector.size() < 20) {
                mytank.attack();
            }
        }
//        else if(e.getKeyCode() == KeyEvent.VK_K){
//            if(enemyTankVector.size()==1){
//                mytank.skill();
//            }
//        }
        repaint();
    }

    public void keyReleased(KeyEvent e) {
    }

    @Override
    public void run() {
        while(true){
            try{
                Thread.sleep(50);
            }
            catch (InterruptedException e){
                throw new RuntimeException(e);
            }
            repaint();
        }
    }

    public void check(Bullet bullet) {
        if(bullet.getX() < 0 || bullet.getX() > 1495 || bullet.getY() < 0 || bullet.getY() > 780){
            bullet.setLeft(false);
        }
    }
    public void checkEnemyTank(EnemyTank enemyTank){
        for(int i = 0; i < mytank.bulletvector.size(); i ++){
            Bullet bullet = mytank.bulletvector.get(i);
            if(bullet.getX() <= enemyTank.getX() + 100 && bullet.getX() >= enemyTank.getX() - 7){
                if(bullet.getY() >= enemyTank.getY() - 7 && bullet.getY() <= enemyTank.getY() + 100){
                    bullet.setLeft(false);
                    enemyTank.setLife(false);
                    break;
                }
            }
        }
        if(enemyTankVector.size()==1){
            if(mytank.getX() <= enemyTank.getX() + 100 && mytank.getX() >= enemyTank.getX() - 100){
                if(mytank.getY() >= enemyTank.getY() - 100 && mytank.getY() <= enemyTank.getY() + 100){
                    enemyTank.setLife(false);
                }
            }
        }
    }
    public void checkMyTank(MyTank mytank){
        for(int j = 0; j < enemyTankVector.size(); j ++){
            EnemyTank enemyTank = (EnemyTank)this.enemyTankVector.get(j);
            for(int i = 0; i < enemyTank.bulletvector.size(); i ++){
                Bullet bullet = enemyTank.bulletvector.get(i);
                if(bullet.getX() <= mytank.getX() + 100 && bullet.getX() >= mytank.getX() - 7){
                    if(bullet.getY() >= mytank.getY() - 7 && bullet.getY() <= mytank.getY() + 100) {
                        bullet.setLeft(false);
                        mytank.setLife(false);
                        break;
                    }
                }
            }
        }
    }
}
